#include <iostream>
struct Node {
	int value;
	Node* next;
};

void reverse(Node *& head) {
	Node * curr = head;
	if (!curr)
		return;
	Node *next = curr->next;
	curr->next = nullptr;
	Node *next2;
	while (next != nullptr) {
		next2 = next->next;
		next->next = curr;
		curr = next;
		next = next2;
	}
	head = curr;
}

void print(Node * head) {
	while(head) {
		std::cout << head->value << ' ';
		head = head->next;
	}
	std::cout << std::endl;
}

Node* create_list(int n) {
	Node * curr = nullptr;
	for (int i = n; i>0; i--) {
		Node * new_node = new Node;
		new_node->next = curr;
		new_node->value = i;
		curr = new_node;
	}
	return curr;
}

void test_singlylinkedlist(){
	while(true) {
		int n;
		std::cout << "enter the number of nodes[negative number to quit]:\n";
		std::cin >> n;
		if (n < 0)
			break;
		Node * head = create_list(n);
		print(head);
		reverse(head);
		print(head);
	}
}